<template>
  <div class="excel">
    <upload-excel :beforeUpload="before" :onSuccess="onSuccess"></upload-excel>
  </div>
</template>

<script>
import { importEmp } from "@/api/employees";
// 导入过滤器
import { formatDate } from "@/filters";
export default {
  name: "import",
  methods: {
    before(file) {
      if (file.size > 2 * 1024 * 1024) {
        this.$message.error("文件的大小不能超过2M");
        return false;
      }
      return true;
    },
    async onSuccess({ results }) {
      let changeKey = {
        姓名: "username",
        手机号: "mobile",
        工号: "workNumber",
        部门: "departmentName",
        入职时间: "timeOfEntry",
        聘用形式: "formOfEmployment",
        转正时间: "correctionTime"
      };

      console.log(results);
      let arr = [];
      results.forEach(item => {
        let obj = {};
        Object.keys(item).forEach(key => {
          if (key == "入职时间" || key == "转正时间") {
            let newDate = new Date((item[key] + 1) * 24 * 60 * 60 * 1000);
            newDate.setFullYear(newDate.getFullYear() - 70);
            console.log(newDate);
            obj[changeKey[key]] = newDate;
          } else {
            obj[changeKey[key]] = item[key];
          }
        });
        arr.push(obj);
      });

      await importEmp(arr);
      this.$router.back();
    }
  }
};
</script>

<style></style>
